<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="招考标题" prop="recruitTitle">
        <el-input v-model="queryParams.recruitTitle" placeholder="请输入招考标题" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="报名时间" prop="registrationTime">
        <el-input
          v-model="queryParams.registrationTime"
          placeholder="请输入报名时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试序号" prop="examNumber">
        <el-input
          v-model="queryParams.examNumber"
          placeholder="请输入考试序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="releaseTime">
        <el-date-picker clearable
          v-model="queryParams.releaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="招聘单位" prop="employer">
        <el-input
          v-model="queryParams.employer"
          placeholder="请输入招聘单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="缴费结束时间" prop="payEndTime">
        <el-date-picker clearable
          v-model="queryParams.payEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择缴费结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学历要求" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄要求" prop="ageRequire">
        <el-input
          v-model="queryParams.ageRequire"
          placeholder="请输入年龄要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:recuit:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:recuit:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:recuit:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:recuit:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recuitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="招聘考试名称" align="center" prop="recruitTitle" />
      <!-- <el-table-column label="报名时间" align="center" prop="registrationTime" /> -->
      <el-table-column label="考试序号" align="center" prop="examNumber" />
      <!-- <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.releaseTime }}</span>
        </template>
</el-table-column> -->
      <!-- <el-table-column label="报名开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="企业类型" align="center" prop="enterpriseType" /> -->
      <!-- <el-table-column label="公告详情" align="center" prop="specifics" /> -->
      <!-- <el-table-column label="招聘单位" align="center" prop="employer" /> -->
      <!-- <el-table-column label="招聘岗位数量" align="center" prop="stationNumber" /> -->
      <el-table-column label="进度类型" align="center" prop="rateType">
        <template slot-scope="scope">
          <span v-if="scope.row.rateType == 0">未开始</span>
          <span v-if="scope.row.rateType == 1">报名中</span>
          <span v-if="scope.row.rateType == 2">公示中</span>
          <span v-if="scope.row.rateType == 3">已结束</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位需求" align="center" prop="xx" />
      <el-table-column label="报名人数" align="center" prop="aa" />
      <el-table-column label="缴费人数" align="center" prop="bb" />

      <!-- <el-table-column label="考试类型" align="center" prop="examType">
        <template slot-scope="scope">
          <span>{{ scope.row.examType == 0 ? '免费' : '收费' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费结束时间" align="center" prop="payEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.payEndTime }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="学历要求" align="center" prop="education" /> -->
      <!-- <el-table-column label="年龄要求" align="center" prop="ageRequire">
        <template slot-scope="scope">
          <span>{{ scope.row.ageRequire == 0 ? '无' : '35岁以下' }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="发布管理" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.releaseStatus" :active-value="1" :inactive-value="0"
            @change="handleChangeStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="岗位上传文件" align="center" prop="positionUrl" /> -->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="380">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:recuit:edit']">修改</el-button> -->
         
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleMangeExamine(scope.row)"
            v-hasPermi="['system:recuit:edit']">考试管理</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:recuit:edit']">考试信息管理</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:recuit:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人才招考信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="招考标题" prop="recruitTitle">
          <el-input v-model="form.recruitTitle" placeholder="请输入招考标题" />
        </el-form-item>
        <el-form-item label="招考单位" prop="employer">
          <el-input v-model="form.employer" placeholder="请输入招聘单位" />
        </el-form-item>
        <el-form-item label="单位类型" prop="enterpriseType">
          <el-select v-model="form.enterpriseType" placeholder="请选择单位类型">
            <el-option v-for="(item, index) in companyTypeList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
          <!-- <el-input v-model="form.employer" placeholder="请输入招聘单位" /> -->
        </el-form-item>
        <el-form-item label="考试序号" prop="examNumber">
          <el-input v-model="form.examNumber" placeholder="请输入考试序号" />
        </el-form-item>
        <!-- <el-form-item label="报名时间" prop="registrationTime">
          <el-input v-model="form.registrationTime" placeholder="请输入报名时间" />
        </el-form-item> -->

        <el-form-item label="考试发布时间" prop="releaseTime">
          <el-date-picker clearable v-model="form.releaseTime" type="datetime" value-format="yyyy-MM-dd hh:mm"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd hh:mm"
            placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd hh:mm"
            placeholder="请选择报名结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="考试类型" prop="examType">
          <el-select v-model="form.examType" placeholder="请选择考试类型" @change="handleChangeExamType">
            <el-option v-for="(item, index) in examineTypeList" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="全日制教育要求" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历">
            <el-option v-for="(item, index) in stationEduList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄要求" prop="ageRequire">
          <el-select v-model="form.ageRequire" placeholder="请选择考试类型">
            <el-option v-for="(item, index) in ageTypeList" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="招聘岗位数量" prop="stationNumber">
          <el-input v-model="form.stationNumber" placeholder="请输入招聘岗位数量" />
        </el-form-item> -->

        <el-form-item label="缴费截止时间" prop="payEndTime">
          <el-date-picker clearable v-model="form.payEndTime" type="datetime" value-format="yyyy-MM-dd hh:mm"
            placeholder="请选择缴费结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="招考简介" prop="specifics">
          <editor v-model="form.specifics" :min-height="192" />
        </el-form-item>

        <el-form-item label="收款码" prop="payQrcode" v-if="form.examType === 1">
          <image-upload :limit="1" v-model="form.payQrcode" />
        </el-form-item>


        <el-form-item label="岗位清单" prop="positionUrl">
          <file-upload v-model="form.positionUrl" :limit="1" />
        </el-form-item>
        <el-divider content-position="center">添加岗位</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddRecruitPosition">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="handleDeleteRecruitPosition">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="recruitPositionList" :row-class-name="rowRecruitPositionIndex"
          @selection-change="handleRecruitPositionSelectionChange" ref="recruitPosition">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="招聘岗位" prop="positionName">
            <template slot-scope="scope">
              <el-input v-model="scope.row.positionName" placeholder="请输入岗位名称" />
            </template>
          </el-table-column>
          <el-table-column label="招聘数量" prop="positionNumber">
            <template slot-scope="scope">
              <el-input v-model="scope.row.positionNumber" placeholder="请输入岗位数量" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="岗位状态" prop="positionStatus">
            <template slot-scope="scope">
              <el-select v-model="scope.row.positionStatus" placeholder="请选择岗位状态">
                <el-option label="正常" value="0" />
                <el-option label="取消" value="1" />
              </el-select>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 考试管理 -->
    <el-dialog :title="examineManageTitle" v-if="examineManageFlag" :visible.sync="examineManageFlag" width="70%"
      append-to-body>
      <examineManage :careerId="recId" :examType="examType"></examineManage>
    </el-dialog>
  </div>
</template>

<script>
import examineManage from './examineManage.vue'
import { getHireDicts } from '@/api/system/dict/data'
import { listRecuit, getRecuit, delRecuit, addRecuit, updateRecuit, enrollList, enrollInfoList } from "@/api/system/recuit";
const examineTypeList = [{
  label: '免费',
  value: 0
}, {
  label: '收费',
  value: 1
}]

const ageTypeList = [{
  label: '无',
  value: '0'
}, {
  label: '35岁以下',
  value: '1'
}]

export default {
  name: "Recuit",
  data() {
    return {
      ageTypeList,
      examineTypeList,
      stationEduList: [],
      companyTypeList: [],
      recId: '',
      examType: '',
      examineManageFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRecruitPosition: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人才招考信息表格数据
      recuitList: [],
      // 招考岗位信息表格数据
      recruitPositionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recruitTitle: null,
        registrationTime: null,
        examNumber: null,
        releaseTime: null,
        startTime: null,
        endTime: null,
        enterpriseType: null,
        specifics: null,
        employer: null,
        stationNumber: null,
        rateType: null,
        examType: null,
        payEndTime: null,
        education: null,
        ageRequire: null,
        positionUrl: null
      },
      // 表单参数
      form: {},
      examineManageTitle: '',
      // 表单校验  enterpriseType
      rules: {
        recruitTitle: [
          { required: true, message: "招考标题不能为空", trigger: "blur" }
        ],
        employer: [
          { required: true, message: "招考单位不能为空", trigger: "blur" }
        ],
        enterpriseType: [
          { required: true, message: "单位类型不能为空", trigger: "blur" }
        ],
        registrationTime: [
          { required: true, message: "报名时间不能为空", trigger: "blur" }
        ],
        releaseTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "报名开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "报名结束时间不能为空", trigger: "blur" }
        ],
        rateType: [
          { required: true, message: "进度类型  0未开始  1报名中 2公示中 3已结束不能为空", trigger: "change" }
        ],
      }
    };
  },
  components: {
    examineManage
  },
  created() {
    this.getList();
    this.getHireDicts();
  },
  methods: {
    /** 查询人才招考信息列表 */
    getList() {
      this.loading = true;
      listRecuit(this.queryParams).then(response => {
        this.recuitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleChangeStatus(item) {
      console.log(item.releaseStatus, 'item.releaseStatus')
      // item.releaseStatus = item.releaseStatus == 1 ? 0 : 1
      updateRecuit(item).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    // 获取字典
    getHireDicts() {
      getHireDicts().then(res => {
        this.stationEduList = res.data.filter(item => item.field === 'stationEdu')[0].dicts
        this.companyTypeList = res.data.filter(item => item.field === 'compType')[0].dicts
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recruitTitle: null,
        registrationTime: null,
        examNumber: null,
        releaseTime: null,
        startTime: null,
        endTime: null,
        enterpriseType: null,
        specifics: null,
        payQrcode: null,
        employer: null,
        stationNumber: null,
        rateType: null,
        examType: null,
        payEndTime: null,
        education: null,
        ageRequire: null,
        positionUrl: null
      };
      this.recruitPositionList = [];
      this.resetForm("form");
    },
    handleChangeExamType(val) {
      console.log(val, 'handleChangeExamType')
      if (val === 0) {
        this.form.payQrcode = ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人才招考信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecuit(id).then(response => {
        this.form = response.data;
        this.recruitPositionList = response.data.recruitPositionList;
        this.open = true;
        this.title = "修改人才招考信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.recruitPositionList = this.recruitPositionList;
          if (this.form.id != null) {
            updateRecuit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecuit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人才招考信息编号为"' + ids + '"的数据项？').then(function () {
        return delRecuit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 考试管理 */
    handleMangeExamine(row) {
      this.recId = row.id
      this.examType = row.examType
      this.examineManageFlag = true
      this.examineManageTitle = row.recruitTitle
    },


    /** 考试信息管理 */
    handleMangeExamineInfo() {


    },

    /** 招考岗位信息序号 */
    rowRecruitPositionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 招考岗位信息添加按钮操作 */
    handleAddRecruitPosition() {
      let obj = {};
      obj.positionName = "";
      obj.positionNumber = "";
      obj.positionStatus = "";
      this.recruitPositionList.push(obj);
    },
    /** 招考岗位信息删除按钮操作 */
    handleDeleteRecruitPosition() {
      if (this.checkedRecruitPosition.length == 0) {
        this.$modal.msgError("请先选择要删除的招考岗位信息数据");
      } else {
        const recruitPositionList = this.recruitPositionList;
        const checkedRecruitPosition = this.checkedRecruitPosition;
        this.recruitPositionList = recruitPositionList.filter(function (item) {
          return checkedRecruitPosition.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleRecruitPositionSelectionChange(selection) {
      this.checkedRecruitPosition = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/recuit/export', {
        ...this.queryParams
      }, `recuit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
